const service = (table) => {
    return `
        import { Inject, Provide } from '@midwayjs/core';
        import { Context } from '@midwayjs/koa';
        import { ${table.da}Entity } from '@entity/${table.folder}/${table.xiao}.entity';
        import {
          List${table.da}DTO,
          Create${table.da}DTO,
          Update${table.da}DTO,
        } from '@dto/${table.folder}/${table.xiao}.dto';
        import { resBuild } from '@utils/resBuild';
        import { InjectEntityModel } from '@midwayjs/typeorm';
        import { Repository } from 'typeorm';
        import { JwtService } from '@midwayjs/jwt';
        import { getTimeFormat, getUUID } from '@utils';
        import { UserService } from '@service/system/user.service';
        import { ListUserDTO } from '@dto/system/user.dto';
        
        @Provide()
        export class ${table.da}Service {
          @Inject()
          ctx: Context;
        
          @InjectEntityModel(${table.da}Entity)
          ${table.da}Entity: Repository<${table.da}Entity>;

          @Inject()
          jwtService: JwtService;
          
          @Inject()
          userService: UserService;

          async getUserId() {
            // 从请求头或请求参数中获取token
            const token = this.ctx.headers.authorization?.split(' ')[1]; // 假设token在Authorization头中，格式为Bearer <token>
            const decoded: any = await this.jwtService.verify(token, {
              complete: true,
            });
            return decoded.payload.userId;
          }
        
          // 列表
          async list(queryParams: List${table.da}DTO) {
            const queryBuilder = this.${table.da}Entity.createQueryBuilder('entity');
        
            // 添加按创建时间排序，默认降序（最新的在前）
            queryBuilder.orderBy('entity.createTime', 'DESC');
        
            if (queryParams.pageNum && queryParams.pageSize) {
              queryBuilder
                .skip((queryParams.pageNum - 1) * queryParams.pageSize)
                .take(queryParams.pageSize);
            }
        
            const [rows, total] = await queryBuilder.getManyAndCount();
            return resBuild.list(rows, total);
          }
        
          // 添加
          async create(${table.da}: Create${table.da}DTO) {
            // 新增
            ${table.da}.userId = await this.getUserId();
            ${table.da}.id = getUUID();;
            const myEntity = this.${table.da}Entity.create(${table.da});
            const { userInfo }: { userInfo: ListUserDTO } =
              await this.userService.userInfo();
            myEntity.setCreateBy(userInfo.nickName);
            myEntity.setUpdateBy(userInfo.nickName);
            myEntity.setCreateTime(getTimeFormat());
            myEntity.setUpdateTime(getTimeFormat());
            await this.${table.da}Entity.save(myEntity);
            return resBuild.success();
          }
        
          // 删除
          async delete(id: string) {
            const ids = id.split(',').map(id => Number(id));
            await this.${table.da}Entity.delete(ids);
            return resBuild.success();
          }
        
          // 修改
          async update(${table.da}: Update${table.da}DTO) {
            const myEntity = this.${table.da}Entity.create(${table.da});
            const userInfo: ListUserDTO = await this.userService.userInfo();
            myEntity.setUpdateBy(userInfo.nickName);
            myEntity.setUpdateTime(getTimeFormat());
            await this.${table.da}Entity.save(myEntity);
            return resBuild.success();
          }
        
          // 详情
          async detail(id: string) {
            const detailInfo = await this.${table.da}Entity.findOneBy({
              id,
            });
            return resBuild.data(detailInfo);
          }
        }
    `
}

export default service
